package com.ruoyi.web.controller.system;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSON;
import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.utils.uuid.UUID;
import com.ruoyi.system.domain.*;
import com.ruoyi.system.service.*;
import com.ruoyi.web.controller.tool.JsonResult;
import com.ruoyi.web.controller.tool.PageInfo;
import org.apache.ibatis.annotations.Param;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import springfox.documentation.spring.web.json.Json;

/**
 * 任务Controller
 * 
 * @author ruoyi
 * @date 2022-12-22
 */
@Controller
@RequestMapping("/system/renwu")
public class RenwuController extends BaseController {
    private String prefix = "system/renwu";

    @Autowired
    private IRenwuService renwuService;

    @Autowired
    private IShiwuService shiwuService;

    @Autowired
    private IDeptService deptService;

    @Autowired
    private IDeptidOlddeptidService deptidOlddeptidService;

    @Autowired
    private IZhuanjiaBiandongDetailService zhuanjiaBiandongDetailService;

    @Autowired
    private IZhuanjiaService zhuanjiaService;

    @Autowired
    private IZhuanjiaQushiquerenService zhuanjiaQushiquerenService;

    @Autowired
    private IJingfeiKaifangService jingfeiKaifangService;

    @RequiresPermissions("system:renwu:view")
    @GetMapping()
    public String renwu() {
        return prefix + "/renwu";
    }

    @GetMapping("/biandongshenhelist")
    public String shenhelist()
    {
        return prefix + "/biandongshenhelist";
    }

    @GetMapping("/zhuanlishenhelist")
    public String zhuanlishenhelist()
    {
        return prefix + "/zhuanlishenhelist";
    }

    /**
     * 查询任务列表
     */
    @RequiresPermissions("system:renwu:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(Renwu renwu) {
        startPage();
        List<Renwu> list = renwuService.selectRenwuList(renwu);
        return getDataTable(list);
    }

    /**
     * 导出任务列表
     */
    @RequiresPermissions("system:renwu:export")
    @Log(title = "任务", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(Renwu renwu) {
        List<Renwu> list = renwuService.selectRenwuList(renwu);
        ExcelUtil<Renwu> util = new ExcelUtil<Renwu>(Renwu.class);
        return util.exportExcel(list, "任务数据");
    }

    /**
     * 新增任务
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 新增保存任务
     */
    @RequiresPermissions("system:renwu:add")
    @Log(title = "任务", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(Renwu renwu) {
        return toAjax(renwuService.insertRenwu(renwu));
    }

    /**
     * 修改任务
     */
    @RequiresPermissions("system:renwu:edit")
    @GetMapping("/edit/{renwuid}")
    public String edit(@PathVariable("renwuid") String renwuid, ModelMap mmap) {
        Renwu renwu = renwuService.selectRenwuByRenwuid(renwuid);
        mmap.put("renwu", renwu);
        return prefix + "/edit";
    }

    /**
     * 修改保存任务
     */
    @RequiresPermissions("system:renwu:edit")
    @Log(title = "任务", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(Renwu renwu) {
        return toAjax(renwuService.updateRenwu(renwu));
    }

    /**
     * 删除任务
     */
    @RequiresPermissions("system:renwu:remove")
    @Log(title = "任务", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(renwuService.deleteRenwuByRenwuids(ids));
    }

    @PostMapping("findone")
    @ResponseBody
    public JsonResult<?> findone(@RequestParam("renwuid")String ppRenwuId){
        try {
            Renwu mmRenwu = renwuService.selectRenwuByRenwuid(ppRenwuId);
            if (mmRenwu==null){
                return JsonResult.getErrorResult("该任务不存在");
            }
            return JsonResult.getSuccessResult(mmRenwu);
        } catch (Exception e) {
            logger.error("renwu -> findone: " + e.getMessage());
            return JsonResult.getErrorResult("查询失败");
        }
    }

    @PostMapping("/submit_custom")
    @ResponseBody
    public JsonResult<?> submit_custom(@RequestParam("id") String ppId, @RequestParam("deptid") String ppDeptId) {
        try {
            boolean checkKaifangZJZX = jingfeiKaifangService.checkKaifangZJZX(ppDeptId);
            if (!checkKaifangZJZX){
                return JsonResult.getErrorResult("提交失败，不在申报时间内。");
            }

            ZhuanjiaBiandongDetail mmZhuanjiabiandongDetail = zhuanjiaBiandongDetailService.selectZhuanjiaBiandongDetailById(ppId);
            String mmBiandongleixing = mmZhuanjiabiandongDetail.getBiandongleixing();

            Shiwu mmShiwu = new Shiwu();
            mmShiwu.setDeptid(ppDeptId);
            mmShiwu.setIsqidong((long) 1);
            mmShiwu.setIsjieshu((long) 0);
            mmShiwu.setChuangjianshijian(new Date());
            mmShiwu.setQidongshijian(new Date());
            mmShiwu.setBeizhu1(ppId);
            mmShiwu.setIsvalid((long) 1);

            Renwu mmRenwu = new Renwu();
            mmRenwu.setRenwuid(UUID.randomUUID().toString());
            String mmOldDeptId = deptidOlddeptidService.DeptIdToOldDeptId(ppDeptId);
            String mmOldParentDeptId = deptService.selectDeptById(mmOldDeptId).getParentid();
            //关联表中将老单位转为新单位ID
            String mmParentDeptId = deptidOlddeptidService.OldDeptIdToDeptId(mmOldParentDeptId);

//            if (mmBiandongleixing.equals("8")){
//                String mmShenpidanweiId = deptidOlddeptidService.OldDeptIdToDeptId(mmZhuanjiabiandongDetail.getZhuanzhidanwei());
//                mmRenwu.setShenpidanweiid(mmShenpidanweiId);
//            }else{
                mmRenwu.setShenpidanweiid(mmParentDeptId);
//            }

            mmRenwu.setRenwuxiafashijian(new Date());
            mmRenwu.setShifouwancheng((long) 0);
            mmRenwu.setShifoutongguo((long) 0);
            mmRenwu.setIsvalid((long) 1);

            List<Map<String,Object>> mmShiwuList = shiwuService.existShenhezhongShiwuByZhuanjiaId(mmZhuanjiabiandongDetail.getZhuanjiaid());
            if(mmShiwuList.size()>0){
                return JsonResult.getErrorResult("该专家有正在审核的变动，请勿重复提交");
            }

            return renwuService.submit_custom(mmRenwu, mmShiwu, mmZhuanjiabiandongDetail) == true ? JsonResult.getSuccessResult("提交成功") : JsonResult.getErrorResult("提交失败");
        } catch (Exception e) {
            logger.error("renwu -> submit_custom: " + e.getMessage());
            return JsonResult.getErrorResult("提交任务失败");
        }
    }

    @PostMapping("/submit_hezeng")
    @ResponseBody
    public JsonResult<?> submit_hezeng(@RequestParam("id") String ppId, @RequestParam("deptid") String ppDeptId){
        try {

            Shiwu mmShiwu = new Shiwu();
            mmShiwu.setDeptid(ppDeptId);
            mmShiwu.setIsqidong((long) 1);
            mmShiwu.setIsjieshu((long) 0);
            mmShiwu.setChuangjianshijian(new Date());
            mmShiwu.setQidongshijian(new Date());
            mmShiwu.setBeizhu1(ppId);
            mmShiwu.setIsvalid((long) 1);

            Renwu mmRenwu = new Renwu();
            mmRenwu.setRenwuid(UUID.randomUUID().toString());
            String mmOldDeptId = deptidOlddeptidService.DeptIdToOldDeptId(ppDeptId);
            String mmOldParentDeptId = deptService.selectDeptById(mmOldDeptId).getParentid();
            //关联表中将老单位转为新单位ID
            String mmParentDeptId = deptidOlddeptidService.OldDeptIdToDeptId(mmOldParentDeptId);
            mmRenwu.setShenpidanweiid(mmParentDeptId);
            mmRenwu.setRenwuxiafashijian(new Date());
            mmRenwu.setShifouwancheng((long) 0);
            mmRenwu.setShifoutongguo((long) 0);
            mmRenwu.setIsvalid((long) 1);

            ZhuanjiaBiandongDetail mmZhuanjiabiandongDetail = zhuanjiaBiandongDetailService.selectZhuanjiaBiandongDetailById(ppId);

            //核增的专家
            Zhuanjia mmZhuanjia = zhuanjiaService.selectZhuanjiaByZhuanjiaid(ppId);
            String mmZhengjianhaoma = mmZhuanjia.getZhengjianhaoma();

            return renwuService.submit_custom(mmRenwu, mmShiwu, mmZhuanjiabiandongDetail) == true ? JsonResult.getSuccessResult("提交成功") : JsonResult.getErrorResult("提交失败");
        } catch (Exception e) {
            logger.error("renwu -> submit_hezeng: " + e.getMessage());
            return JsonResult.getErrorResult("提交任务失败");
        }
    }

    @PostMapping("/piliangshenhe_biandong")
    @ResponseBody
    public JsonResult<?> piliangshenhe_biandong(@RequestParam("renwuids") String ppRenwuids,@RequestParam("shiwuids") String ppShiwuids,@RequestParam("ids") String ppIds,@RequestParam("shenherenid")String ppShenherenId,@RequestParam("shenheren")String ppShenheren,
                                                @RequestParam("shifoutongguo") Long ppShifoutongguo,@RequestParam("shenheyijian")String ppShenheyijian,
                                                @RequestParam("deptid") String ppDeptId){
        try{

            int count = 0;
            boolean ok = true;
            String Zhuanjianame = "";

            String[] renwuids = ppRenwuids.split(";");
            String[] shiwuids = ppShiwuids.split(";");
            String[] ids = ppIds.split(";");

            for(int i=0;i<renwuids.length;i++){

                String mmShiwuid = shiwuids[i];
                String mmRenwuId = renwuids[i];
                String mmId = ids[i];

                Shiwu mmShiwu = shiwuService.selectShiwuByShiwuid(mmShiwuid);
                if (mmShiwu == null) {
                    return JsonResult.getErrorResult("该事务不存在");
                }

                if (ppDeptId.equals(RuoYiConfig.getZjzxdeptid())&&ppShifoutongguo==1){
                    mmShiwu.setIsjieshu((long)1);
                    mmShiwu.setJieshushijian(new Date());
                }

                //驳回到本人
                if (ppShifoutongguo==2){
                    mmShiwu.setIsqidong((long)0);
                }

                Renwu mmRenwu1 = renwuService.selectRenwuByRenwuid(mmRenwuId);
                mmRenwu1.setShifoutongguo(ppShifoutongguo);
                mmRenwu1.setShenpiyijian(ppShenheyijian);
                mmRenwu1.setShifouwancheng((long)1);
                mmRenwu1.setRenwuwanchengshijian(new Date());
                mmRenwu1.setShenpirenid(ppShenherenId);
                mmRenwu1.setShenpiren(ppShenheren);

                Renwu mmRenwu2 = new Renwu();

                //非专家中心才有下一个任务
                if (!ppDeptId.equals(RuoYiConfig.getZjzxdeptid())){

                    mmRenwu2.setRenwuid(UUID.randomUUID().toString());
                    mmRenwu2.setShiwuid(mmShiwuid);

                    String mmOldDeptId = deptidOlddeptidService.selectDeptidOlddeptidByDeptid(ppDeptId).getOlddeptid();
                    String mmOldParentDeptId = deptService.selectDeptById(mmOldDeptId).getParentid();
                    //关联表中将老单位转为新单位ID
                    DeptidOlddeptid mmDeptidOlddeptid = new DeptidOlddeptid();
                    mmDeptidOlddeptid.setOlddeptid(mmOldParentDeptId);
                    String mmDeptId = deptidOlddeptidService.selectDeptidOlddeptidList(mmDeptidOlddeptid).get(0).getDeptid();
                    mmRenwu2.setShenpidanweiid(mmDeptId);
                    mmRenwu2.setShangyijishenpidanweiid(ppDeptId);
                    mmRenwu2.setRenwuxiafashijian(new Date());
                    mmRenwu2.setShifouwancheng((long) 0);
                    mmRenwu2.setOrdernum(renwuService.findMaxOrdernumByShiwuId(mmShiwuid)+1);
                    mmRenwu2.setShifoutongguo((long) 0);
                    mmRenwu2.setIsvalid((long) 1);
                }

                //专家变动详情
                ZhuanjiaBiandongDetail mmZhuanjiazhuanliDetail = zhuanjiaBiandongDetailService.selectZhuanjiaBiandongDetailById(mmId);

                if (renwuService.shenhe(mmZhuanjiazhuanliDetail,mmRenwu1,mmRenwu2, mmShiwu)==true){
                    count++;
                }else{
                    ok = false;
                    Zhuanjianame = mmZhuanjiazhuanliDetail.getZhuanjiamingcheng()+";";
                }

            }
            return ok==true?JsonResult.getSuccessResult(count+"个专家变动审核完成。"): JsonResult.getErrorResult(Zhuanjianame+"的专家变动审核失败。");
        }catch (Exception e){
            logger.error("renwu -> piliangshenhe_biandong: " + e.getMessage());
            return JsonResult.getErrorResult("批量审核失败");
        }
    }

    @PostMapping("/shenhe_biandong")
    @ResponseBody
    public JsonResult<?> shenhe_biandong(@RequestParam("id")String ppId,@RequestParam("shiwuid") String ppShiwuid,@RequestParam("renwuid") String ppRenwuId,
                                @RequestParam("shenherenid")String ppShenherenId,@RequestParam("shenheren")String ppShenheren,
                                @RequestParam("shifoutongguo") Long ppShifoutongguo,@RequestParam("shenheyijian")String ppShenheyijian,
                                @RequestParam("deptid") String ppDeptId) {
        try {


            Shiwu mmShiwu = shiwuService.selectShiwuByShiwuid(ppShiwuid);
            if (mmShiwu == null) {
                return JsonResult.getErrorResult("该事务不存在");
            }

            if (ppDeptId.equals(RuoYiConfig.getZjzxdeptid())&&ppShifoutongguo==1){
                mmShiwu.setIsjieshu((long)1);
                mmShiwu.setJieshushijian(new Date());
            }

            //驳回到本人
            if (ppShifoutongguo==2){
                mmShiwu.setIsqidong((long)0);
            }

            Renwu mmRenwu1 = renwuService.selectRenwuByRenwuid(ppRenwuId);
            mmRenwu1.setShifoutongguo(ppShifoutongguo);
            mmRenwu1.setShenpiyijian(ppShenheyijian);
            mmRenwu1.setShifouwancheng((long)1);
            mmRenwu1.setRenwuwanchengshijian(new Date());
            mmRenwu1.setShenpirenid(ppShenherenId);
            mmRenwu1.setShenpiren(ppShenheren);

            Renwu mmRenwu2 = new Renwu();

            //非专家中心才有下一个任务
            if (!ppDeptId.equals(RuoYiConfig.getZjzxdeptid())){

                mmRenwu2.setRenwuid(UUID.randomUUID().toString());
                mmRenwu2.setShiwuid(ppShiwuid);

                String mmOldDeptId = deptidOlddeptidService.selectDeptidOlddeptidByDeptid(ppDeptId).getOlddeptid();
                String mmOldParentDeptId = deptService.selectDeptById(mmOldDeptId).getParentid();
                //关联表中将老单位转为新单位ID
                DeptidOlddeptid mmDeptidOlddeptid = new DeptidOlddeptid();
                mmDeptidOlddeptid.setOlddeptid(mmOldParentDeptId);
                String mmDeptId = deptidOlddeptidService.selectDeptidOlddeptidList(mmDeptidOlddeptid).get(0).getDeptid();
                mmRenwu2.setShenpidanweiid(mmDeptId);
                mmRenwu2.setShangyijishenpidanweiid(ppDeptId);
                mmRenwu2.setRenwuxiafashijian(new Date());
                mmRenwu2.setShifouwancheng((long) 0);
                mmRenwu2.setOrdernum(renwuService.findMaxOrdernumByShiwuId(ppShiwuid)+1);
                mmRenwu2.setShifoutongguo((long) 0);
                mmRenwu2.setIsvalid((long) 1);
            }

            //专家变动详情
            ZhuanjiaBiandongDetail mmZhuanjiaBiandongDetail = zhuanjiaBiandongDetailService.selectZhuanjiaBiandongDetailById(ppId);

            return renwuService.shenhe(mmZhuanjiaBiandongDetail,mmRenwu1,mmRenwu2, mmShiwu) == true ? JsonResult.getSuccessResult("审核成功") : JsonResult.getErrorResult("审核失败");
        } catch (Exception e) {
            logger.error("renwu -> shenhe: " + e.getMessage());
            return JsonResult.getErrorResult("审核失败");
        }
    }

    //转隶审核
//    @PostMapping("/shenhe_zhuanli")
//    @ResponseBody
//    public JsonResult<?> shenhe_zhuanli(@RequestParam("id")String ppId,@RequestParam("shiwuid") String ppShiwuid,@RequestParam("renwuid") String ppRenwuId,
//                                         @RequestParam("shenherenid")String ppShenherenId,@RequestParam("shenheren")String ppShenheren,
//                                         @RequestParam("shifoutongguo") Long ppShifoutongguo,@RequestParam("shenheyijian")String ppShenheyijian,
//                                         @RequestParam("deptid") String ppDeptId) {
//        try {
//
//            //判断该事务是否存在
//            Shiwu mmShiwu = shiwuService.selectShiwuByShiwuid(ppShiwuid);
//            if (mmShiwu == null) {
//                return JsonResult.getErrorResult("该事务不存在");
//            }
//
//            //专家变动详情
//            ZhuanjiaBiandongDetail mmZhuanjiazhuanliDetail = zhuanjiaBiandongDetailService.selectZhuanjiaBiandongDetailById(ppId);
//            //转自单位
//            String mmZhuanzidanweiId = mmZhuanjiazhuanliDetail.getZhuanzidanwei();
//            //转至单位
//            String mmZhuanzhidanweiId = mmZhuanjiazhuanliDetail.getZhuanzhidanwei();
//
//            //转自单位省份单位
//            Dept mmZhuanzidanweiShengfenDept = deptService.findJibie1(mmZhuanzidanweiId);
//            String mmZhuanziShengfenDeptId = mmZhuanzidanweiShengfenDept.getId();
//            //转至单位省份单位
//            Dept mmZhuanzhidanweiShengfenDept = deptService.findJibie1(mmZhuanzhidanweiId);
//            String mmZhuanzhiShengfenDeptId = mmZhuanzhidanweiShengfenDept.getId();
//
//            System.out.println("转自单位省份："+mmZhuanziShengfenDeptId);
//            System.out.println("转至单位省份："+mmZhuanzhiShengfenDeptId);
//
//            String mmOldDeptId = deptidOlddeptidService.selectDeptidOlddeptidByDeptid(ppDeptId).getOlddeptid();
//
//            //如果审核单位是转至单位并通过审核，则该事务结束
//            if (mmOldDeptId.equals(mmZhuanzhidanweiId)&&ppShifoutongguo==1){
//                mmShiwu.setIsjieshu((long)1);
//                mmShiwu.setJieshushijian(new Date());
//            }
//
//            //若不通过，该事务驳回到本人
//            if (ppShifoutongguo==2){
//                mmShiwu.setIsqidong((long)0);
//            }
//
//            //审核单位的任务，修改
//            Renwu mmRenwu1 = renwuService.selectRenwuByRenwuid(ppRenwuId);
//            mmRenwu1.setShifoutongguo(ppShifoutongguo);
//            mmRenwu1.setShenpiyijian(ppShenheyijian);
//            mmRenwu1.setShifouwancheng((long)1);
//            mmRenwu1.setRenwuwanchengshijian(new Date());
//            mmRenwu1.setShenpirenid(ppShenherenId);
//            mmRenwu1.setShenpiren(ppShenheren);
//
//            //创建新任务给下一个审核单位
//            Renwu mmRenwu2 = new Renwu();
//            mmRenwu2.setRenwuid(UUID.randomUUID().toString());
//            mmRenwu2.setShiwuid(ppShiwuid);
//            mmRenwu2.setShangyijishenpidanweiid(ppDeptId);
//            mmRenwu2.setRenwuxiafashijian(new Date());
//            mmRenwu2.setShifouwancheng((long) 0);
//            mmRenwu2.setOrdernum(renwuService.findMaxOrdernumByShiwuId(ppShiwuid)+1);
//            mmRenwu2.setShifoutongguo((long) 0);
//            mmRenwu2.setIsvalid((long) 1);
//
//            //审核单位
//            Dept mmDept = deptService.selectDeptById(mmOldDeptId);
//            //机构级别
//            String mmJigoujibie = mmDept.getJigoujibie();
//            //如果1.当前审核单位是转自单位省份单位，2.机构级别是省份，3.转自单位转至单位同一省份单位，则下一级审核单位为转至单位
//            if (mmOldDeptId.equals(mmZhuanziShengfenDeptId)&&mmJigoujibie.equals("1")&&mmZhuanziShengfenDeptId.equals(mmZhuanzhiShengfenDeptId)){
//                mmRenwu2.setShenpidanweiid(olddeptidToDeptid(mmZhuanzhidanweiId));
//            }
//            //如果1，2，3.转自单位转至单位不同省份单位，则下一级审核单位为转至单位省份单位
//            else if(mmOldDeptId.equals(mmZhuanziShengfenDeptId)&&mmJigoujibie.equals("1")&&!mmZhuanziShengfenDeptId.equals(mmZhuanzhiShengfenDeptId)){
//                mmRenwu2.setShenpidanweiid(olddeptidToDeptid(mmZhuanzhiShengfenDeptId));
//            }
//            //如果1.当前审核单位为转至单位省份单位，2，3.转自单位转至单位不同省份单位，则下一级审核单位为转至单位
//            else if(mmOldDeptId.equals(mmZhuanzhiShengfenDeptId)&&mmJigoujibie.equals("1")&&!mmZhuanziShengfenDeptId.equals(mmZhuanzhiShengfenDeptId)){
//                mmRenwu2.setShenpidanweiid(olddeptidToDeptid(mmZhuanzhidanweiId));
//            }
//            else{
//                mmRenwu2.setShenpidanweiid(olddeptidToDeptid(deptService.selectDeptById(mmOldDeptId).getParentid()));
//            }
//
//            return renwuService.shenhe_zhuanli(mmZhuanjiazhuanliDetail,mmRenwu1,mmRenwu2, mmShiwu) == true ? JsonResult.getSuccessResult("审核成功") : JsonResult.getErrorResult("审核失败");
//        } catch (Exception e) {
//            logger.error("renwu -> shenhe_zhuanli: " + e.getMessage());
//            return JsonResult.getErrorResult("审核失败");
//        }
//    }

//    @PostMapping("/shenhe_zhuanli")
//    @ResponseBody
//    public JsonResult<?> shenhe_zhuanli(@RequestParam("id")String ppId,@RequestParam("shiwuid") String ppShiwuid,@RequestParam("renwuid") String ppRenwuId,
//                                        @RequestParam("shenherenid")String ppShenherenId,@RequestParam("shenheren")String ppShenheren,
//                                        @RequestParam("shifoutongguo") Long ppShifoutongguo,@RequestParam("shenheyijian")String ppShenheyijian,
//                                        @RequestParam("deptid") String ppDeptId) {
//        try {
//
//            //判断该事务是否存在
//            Shiwu mmShiwu = shiwuService.selectShiwuByShiwuid(ppShiwuid);
//            if (mmShiwu == null) {
//                return JsonResult.getErrorResult("该事务不存在");
//            }
//
//            //专家变动详情
//            ZhuanjiaBiandongDetail mmZhuanjiazhuanliDetail = zhuanjiaBiandongDetailService.selectZhuanjiaBiandongDetailById(ppId);
//            //转自单位
//            String mmZhuanzidanweiId = mmZhuanjiazhuanliDetail.getZhuanzidanwei();
//            //转至单位
//            String mmZhuanzhidanweiId = mmZhuanjiazhuanliDetail.getZhuanzhidanwei();
//
//            String mmOldDeptId = deptidOlddeptidService.selectDeptidOlddeptidByDeptid(ppDeptId).getOlddeptid();
//
//            //如果审核单位是转至单位并通过审核，则该事务结束
//            if (mmOldDeptId.equals(mmZhuanzhidanweiId)&&ppShifoutongguo==1){
//                mmShiwu.setIsjieshu((long)1);
//                mmShiwu.setJieshushijian(new Date());
//            }
//
//            //若不通过，该事务驳回到本人
//            if (ppShifoutongguo==2){
//                mmShiwu.setIsqidong((long)0);
//            }
//
//            //审核单位的任务，修改
//            Renwu mmRenwu = renwuService.selectRenwuByRenwuid(ppRenwuId);
//            mmRenwu.setShifoutongguo(ppShifoutongguo);
//            mmRenwu.setShenpiyijian(ppShenheyijian);
//            mmRenwu.setShifouwancheng((long)1);
//            mmRenwu.setRenwuwanchengshijian(new Date());
//            mmRenwu.setShenpirenid(ppShenherenId);
//            mmRenwu.setShenpiren(ppShenheren);
//
//            //审核单位
//            Dept mmDept = deptService.selectDeptById(mmOldDeptId);
//
//            return renwuService.shenhe_zhuanli(mmZhuanjiazhuanliDetail,mmRenwu, mmShiwu) == true ? JsonResult.getSuccessResult("审核成功") : JsonResult.getErrorResult("审核失败");
//        } catch (Exception e) {
//            logger.error("renwu -> shenhe_zhuanli: " + e.getMessage());
//            return JsonResult.getErrorResult("审核失败");
//        }
//    }

    @PostMapping("/shenhe_zhuanli")
    @ResponseBody
    public JsonResult<?> shenhe_zhuanli(@RequestParam("id")String ppId,@RequestParam("shiwuid") String ppShiwuid,@RequestParam("renwuid") String ppRenwuId,
                                        @RequestParam("shenherenid")String ppShenherenId,@RequestParam("shenheren")String ppShenheren,
                                        @RequestParam("shifoutongguo") Long ppShifoutongguo,@RequestParam("shenheyijian")String ppShenheyijian,
                                        @RequestParam("deptid") String ppDeptId,@RequestParam("isrenling") int ppIsRenling,@RequestParam("zhuanzhidanweiid")String ppZhuanzhidanweiId,@RequestParam("zhuanzhidanweimingcheng")String ppZhuanzhidanweimingcheng) {
        try {

            //判断该事务是否存在
            Shiwu mmShiwu = shiwuService.selectShiwuByShiwuid(ppShiwuid);
            if (mmShiwu == null) {
                return JsonResult.getErrorResult("该事务不存在");
            }

            //专家变动详情
            ZhuanjiaBiandongDetail mmZhuanjiazhuanliDetail = zhuanjiaBiandongDetailService.selectZhuanjiaBiandongDetailById(ppId);


            if (!ppZhuanzhidanweiId.equals("")&&ppZhuanzhidanweiId!=null){
                mmZhuanjiazhuanliDetail.setZhuanzhidanwei(ppZhuanzhidanweiId);
                mmZhuanjiazhuanliDetail.setZhuanzhidanweimingcheng(ppZhuanzhidanweimingcheng);
                System.out.println("aa");
            }

            //转自单位旧ID
            String mmZhuanzidanweiId = mmZhuanjiazhuanliDetail.getZhuanzidanwei();

            //转至单位旧ID
            String mmZhuanzhidanweiId = mmZhuanjiazhuanliDetail.getZhuanzhidanwei();

            System.out.println("zhuanzhidanwei:"+mmZhuanzhidanweiId);
            //审批单位旧ID
            String mmOldDeptId = deptidOlddeptidService.DeptIdToOldDeptId(ppDeptId);

            //如果认领，则专家直接进入本单位
            if (ppIsRenling==1){
                mmShiwu.setIsjieshu((long)1);
                mmShiwu.setJieshushijian(new Date());
            }

            //审核单位的任务，修改
            Renwu mmRenwu = renwuService.selectRenwuByRenwuid(ppRenwuId);
            mmRenwu.setShifoutongguo(ppShifoutongguo);
            mmRenwu.setShenpiyijian(ppShenheyijian);
            mmRenwu.setShifouwancheng((long)1);
            mmRenwu.setRenwuwanchengshijian(new Date());
            mmRenwu.setShenpirenid(ppShenherenId);
            mmRenwu.setShenpiren(ppShenheren);

            //创建新任务给下一个审核单位
            Renwu mmRenwu2 = new Renwu();
            mmRenwu2.setRenwuid(UUID.randomUUID().toString());
            mmRenwu2.setShiwuid(ppShiwuid);
            mmRenwu2.setShangyijishenpidanweiid(ppDeptId);
            mmRenwu2.setRenwuxiafashijian(new Date());
            mmRenwu2.setShifouwancheng((long) 0);
            mmRenwu2.setOrdernum(renwuService.findMaxOrdernumByShiwuId(ppShiwuid)+1);
            mmRenwu2.setShifoutongguo((long) 0);
            mmRenwu2.setIsvalid((long) 1);

            //审核单位
            Dept mmDept = deptService.selectDeptById(mmOldDeptId);

            return renwuService.shenhe_zhuanli(mmZhuanjiazhuanliDetail,mmRenwu,mmRenwu2, mmShiwu,ppIsRenling,mmDept) == true ? JsonResult.getSuccessResult("审核成功") : JsonResult.getErrorResult("审核失败");
        } catch (Exception e) {
            logger.error("renwu -> shenhe_zhuanli: " + e.getMessage());
            return JsonResult.getErrorResult("审核失败");
        }
    }

    @PostMapping("/xiapai")
    @ResponseBody
    public JsonResult<?> xiapai(@RequestParam("id")String ppId,@RequestParam("shiwuid") String ppShiwuid,
                                        @RequestParam("deptid") String ppDeptId,@RequestParam("xiapaidanweiid")String ppXiapaidanweiId) {
        try {

            //判断该事务是否存在
            Shiwu mmShiwu = shiwuService.selectShiwuByShiwuid(ppShiwuid);
            if (mmShiwu == null) {
                return JsonResult.getErrorResult("该事务不存在");
            }

            //专家变动详情
            ZhuanjiaBiandongDetail mmZhuanjiazhuanliDetail = zhuanjiaBiandongDetailService.selectZhuanjiaBiandongDetailById(ppId);

            //审批单位旧ID
            String mmOldDeptId = deptidOlddeptidService.DeptIdToOldDeptId(ppDeptId);

            //审核单位
            Dept mmDept = deptService.selectDeptById(mmOldDeptId);

            return renwuService.xiapai(mmZhuanjiazhuanliDetail,mmShiwu,mmDept,ppXiapaidanweiId) == true ? JsonResult.getSuccessResult("下派成功") : JsonResult.getErrorResult("下派失败");
        } catch (Exception e) {
            logger.error("renwu -> xiapai: " + e.getMessage());
            return JsonResult.getErrorResult("下派失败");
        }
    }

    @PostMapping("/jieshou")
    @ResponseBody
    public JsonResult<?> jieshou(@RequestParam("id")String ppId,@RequestParam("shiwuid") String ppShiwuid,@RequestParam("renwuid") String ppRenwuId,
                                 @RequestParam("shenherenid")String ppShenherenId,@RequestParam("shenheren")String ppShenheren,
                                @RequestParam("deptid") String ppDeptId,@RequestParam("shifoutongguo")long ppShifoutongguo) {
        try {

            //判断该事务是否存在
            Shiwu mmShiwu = shiwuService.selectShiwuByShiwuid(ppShiwuid);
            if (mmShiwu == null) {
                return JsonResult.getErrorResult("该事务不存在");
            }

            //专家变动详情
            ZhuanjiaBiandongDetail mmZhuanjiazhuanliDetail = zhuanjiaBiandongDetailService.selectZhuanjiaBiandongDetailById(ppId);
            //转自单位旧ID
            String mmZhuanzidanweiId = mmZhuanjiazhuanliDetail.getZhuanzidanwei();

            //转至单位旧ID
            String mmZhuanzhidanweiId = mmZhuanjiazhuanliDetail.getZhuanzhidanwei();

            //审批单位旧ID
            String mmOldDeptId = deptidOlddeptidService.DeptIdToOldDeptId(ppDeptId);

            //审核单位的任务，修改
            Renwu mmRenwu = renwuService.selectRenwuByRenwuid(ppRenwuId);
            mmRenwu.setShifoutongguo(ppShifoutongguo);
            if (ppShifoutongguo==1){
                mmRenwu.setShenpiyijian("已接收");
            }else{
                mmRenwu.setShenpiyijian("未接收");
            }
            mmRenwu.setShifouwancheng((long)1);
            mmRenwu.setRenwuwanchengshijian(new Date());
            mmRenwu.setShenpirenid(ppShenherenId);
            mmRenwu.setShenpiren(ppShenheren);

            Renwu mmTempRenwu = new Renwu();
            mmTempRenwu.setShiwuid(ppShiwuid);
            mmTempRenwu.setOrdernum(mmRenwu.getOrdernum()-1);

            //创建新任务给下一个审核单位
            Renwu mmRenwu2 = new Renwu();
            mmRenwu2.setRenwuid(UUID.randomUUID().toString());
            mmRenwu2.setShiwuid(ppShiwuid);
            mmRenwu2.setShangyijishenpidanweiid(renwuService.selectRenwuList(mmTempRenwu).get(0).getShangyijishenpidanweiid());
            mmRenwu2.setRenwuxiafashijian(new Date());
            mmRenwu2.setShifouwancheng((long) 0);
            mmRenwu2.setOrdernum(renwuService.findMaxOrdernumByShiwuId(ppShiwuid)+1);
            mmRenwu2.setShifoutongguo((long) 0);
            mmRenwu2.setIsvalid((long) 1);
            mmRenwu2.setShenpidanweiid(mmRenwu.getShangyijishenpidanweiid());

            if (ppShifoutongguo==1){
                mmShiwu.setLiuchengid("已接收");
            } else if (ppShifoutongguo==0) {
                mmShiwu.setLiuchengid("");
            }

            //审核单位
            Dept mmDept = deptService.selectDeptById(mmOldDeptId);

            String text = "";
            if (ppShifoutongguo==1){
                text = "接收";
            }else if (ppShifoutongguo==0){
                text = "拒收";
            }

            return renwuService.jieshou(mmZhuanjiazhuanliDetail,mmRenwu,mmRenwu2,mmShiwu,mmDept,ppShifoutongguo) == true ? JsonResult.getSuccessResult(text+"成功") : JsonResult.getErrorResult(text+"失败");
        } catch (Exception e) {
            logger.error("renwu -> jieshou: " + e.getMessage());
            return JsonResult.getErrorResult("接收失败");
        }
    }

    @PostMapping("/findshenhejilu")
    @ResponseBody
    public JsonResult<?> findshenhejilu(@RequestParam("shiwuid") String ppShiwuId) {
        try {
            List<Map<String,Object>> mmList = renwuService.findShenhejiluByShiwuid(ppShiwuId);

            return JsonResult.getSuccessResult(mmList);
        } catch (Exception e) {
            logger.error("renwu -> findshenhejilu: " + e.getMessage());
            return JsonResult.getErrorResult("查询审核记录失败");
        }
    }

    @PostMapping("/findshenhelist")
    @ResponseBody
    public JsonResult<?> findshenhelist(@RequestParam("deptid") String ppDeptid,
                                        @RequestParam("shenhezhuangtai")int ppShenhezhuangtai,
                                        @RequestParam("jigoumingcheng")String ppJigoumingcheng,
                                        @RequestParam("zhuanjiaxingming")String ppZhuanjiaxingming,
                                        @RequestParam("renwuxiafashijian1")String ppRenwuxiafashijian1,
                                        @RequestParam("renwuxiafashijian2")String ppRenwuxiafashijian2,
                                        @RequestParam("renwuwanchengshijian1")String ppRenwuwanchengshijian1,
                                        @RequestParam("renwuwanchengshijian2")String ppRenwuwanchengshijian2,
                                        @RequestParam("biandongleixing")String biandongleixing,
                                        @RequestParam("pageindex") int ppPageIndex,
                                        @RequestParam("pagesize") int ppPageSize) {
        try {
            List<Map<String,Object>> list = renwuService.findShenheListByDept(ppDeptid,ppShenhezhuangtai,ppJigoumingcheng,
                     ppZhuanjiaxingming,ppRenwuxiafashijian1,ppRenwuxiafashijian2,ppRenwuwanchengshijian1,ppRenwuwanchengshijian2,biandongleixing,ppPageIndex,ppPageSize);

            int mmCount = renwuService.countShenheListByDept(ppDeptid,ppShenhezhuangtai,ppJigoumingcheng,
                    ppZhuanjiaxingming,ppRenwuxiafashijian1,ppRenwuxiafashijian2,ppRenwuwanchengshijian1,ppRenwuwanchengshijian2,biandongleixing);

            PageInfo mmPageInfo = new PageInfo(ppPageIndex, ppPageSize, mmCount);
            Map<String, Object> mmMap = new HashMap<String, Object>();
            mmMap.put("shenhelist", list);
            mmMap.put("PageInfo", mmPageInfo);
            return JsonResult.getSuccessResult(mmMap);
        } catch (Exception e) {
            logger.error("renwu -> findshenhelist: " + e.getMessage());
            return JsonResult.getErrorResult("查询审核列表失败");
        }
    }

    @PostMapping("/findzhuanlishenhelist")
    @ResponseBody
    public JsonResult<?> findzhuanlishenhelist(@RequestParam("deptid") String ppDeptid,
                                               @RequestParam("shenhezhuangtai")int ppShenhezhuangtai,
                                               @RequestParam("jigoumingcheng")String ppJigoumingcheng,
                                               @RequestParam("zhuanjiaxingming")String ppZhuanjiaxingming,
                                               @RequestParam("renwuxiafashijian1")String ppRenwuxiafashijian1,
                                               @RequestParam("renwuxiafashijian2")String ppRenwuxiafashijian2,
                                               @RequestParam("renwuwanchengshijian1")String ppRenwuwanchengshijian1,
                                               @RequestParam("renwuwanchengshijian2")String ppRenwuwanchengshijian2,
                                               @RequestParam("pageindex") int ppPageIndex,
                                               @RequestParam("pagesize") int ppPageSize) {
        try {
            List<Map<String,Object>> list = renwuService.findZhuanliShenheListByDept(ppDeptid,ppShenhezhuangtai,ppJigoumingcheng,
                    ppZhuanjiaxingming,ppRenwuxiafashijian1,ppRenwuxiafashijian2,ppRenwuwanchengshijian1,ppRenwuwanchengshijian2,ppPageIndex,ppPageSize);

            int mmCount = renwuService.countZhuanliShenheListByDept(ppDeptid,ppShenhezhuangtai,ppJigoumingcheng,
                    ppZhuanjiaxingming,ppRenwuxiafashijian1,ppRenwuxiafashijian2,ppRenwuwanchengshijian1,ppRenwuwanchengshijian2);

            PageInfo mmPageInfo = new PageInfo(ppPageIndex, ppPageSize, mmCount);
            Map<String, Object> mmMap = new HashMap<String, Object>();
            mmMap.put("shenhelist", list);
            mmMap.put("PageInfo", mmPageInfo);
            return JsonResult.getSuccessResult(mmMap);
        } catch (Exception e) {
            logger.error("renwu -> findzhuanlishenhelist: " + e.getMessage());
            return JsonResult.getErrorResult("查询审核列表失败");
        }
    }

    //新旧单位ID转换
    public String deptidToOlddeptid(String ppDeptId){
        return deptidOlddeptidService.selectDeptidOlddeptidByDeptid(ppDeptId).getOlddeptid();
    }

    public String olddeptidToDeptid(String ppOldDeptId){
        DeptidOlddeptid mmOne = new DeptidOlddeptid();
        mmOne.setOlddeptid(ppOldDeptId);
        return deptidOlddeptidService.selectDeptidOlddeptidList(mmOne).get(0).getDeptid();
    }
}
